package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemMapper checkItemMapper;

    /**
     * 新增检查项
     * @param checkItem
     */
    @Override
    public void add(CheckItem checkItem) {

        //checkItemMapper.add(checkItem);
        checkItemMapper.insertSelective(checkItem);

    }

    /**
     * 查询分页数据
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
   /* @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {

        //定义从第几条记录开始查询，limit x，y中的x的值
        int firstResult = (currentPage-1)*pageSize;

        //查询总记录数
        Long count = checkItemMapper.findCountByCondition(queryString);

        //查询分页数据
        List<CheckItem> checkItemList = checkItemMapper.findByCondition(firstResult,pageSize,queryString);

        PageResult pageResult = new PageResult(count,checkItemList);

        return pageResult;
    }*/

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {

        //定义页面大小和当前页页面
        PageHelper.startPage(currentPage,pageSize);

        //条件查询所有检查项数据
        List<CheckItem> checkItems = checkItemMapper.findByCondition(queryString);

        //将查询出来的数据进行包装
        PageInfo<CheckItem> pageInfo = new PageInfo<>(checkItems);


        PageResult pageResult = new PageResult(pageInfo.getTotal(),pageInfo.getList());

        return pageResult;
    }

    /**
     * 通过id删除检查项
     *
     *
     * @param id
     */
    @Override
    public void deleteById(Integer id) {


        //查询检查项有没有被检查组引用
        Integer count = checkItemMapper.findCountByCheckItemId(id);

        if (count>0){
            //如果被引用，抛出异常，提示不能删除
            throw new RuntimeException(MessageConstant.CHECKITEM_IS_QUOTED);
        }
        //如果没有引用，那么就执行删除
        //checkItemMapper.deleteById(id);
        checkItemMapper.deleteByPrimaryKey(id);

    }

    /**
     * 通过id查询检查项
     * @param id
     * @return
     */
    @Override
    public CheckItem findById(Integer id) {
        //return checkItemMapper.findById(id);
        return checkItemMapper.selectByPrimaryKey(id);
    }

    /**
     * 更新检查项
     */
    @Override
    public void edit(CheckItem checkItem) {
        //checkItemMapper.edit(checkItem);
        checkItemMapper.updateByPrimaryKeySelective(checkItem);
    }

    /**
     * 查询所有检查项数据
     */
    @Override
    public List<CheckItem> findAll() {
        //return checkItemMapper.findAll();
        return checkItemMapper.selectAll();
    }

    /**
     * 通过检查组查询对应的检查项的id集合
     */
    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return checkItemMapper.findCheckItemIdsByCheckGroupId(id);
    }
}
